package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.ForumSearchArgument
import com.gin.kotlin.no_gay_alliance.model.ForumSearchResultModel
import com.gin.nga.main.NgaApi
import com.gin.nga.response.forum.ForumInfo
import java.lang.ref.WeakReference

class ForumSearchProvider private constructor(): AbstractCachedDataProvider<ForumSearchArgument, List<ForumInfo>, List<ForumSearchResultModel>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, ForumSearchResultModel::class.java)
) {
    override val dir = "FORUM_SEARCH"

    override fun transform(rawData: List<ForumInfo>) = rawData.map { ForumSearchResultModel(it) }

    override fun requestMethod(ngaApi: NgaApi, params: ForumSearchArgument?) = ngaApi.forumApi.search(params?.keyword ?: "", params?.page)

    override fun obtainRelativePath(userId: Long, params: ForumSearchArgument?) = "${params?.keyword}_${params?.page}.json"

    companion object {
        private var instance: WeakReference<ForumSearchProvider> = WeakReference(ForumSearchProvider())
        fun getInstance(): ForumSearchProvider = instance.get() ?: ForumSearchProvider().also { instance = WeakReference(it) }
    }
}